package com.ee.chat.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.ICoinService;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.UserCoinLogMapper;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Service
public class CoinServiceImpl implements ICoinService {
    @Resource
    UserCoinLogMapper userCoinLogMapper;
    @Resource
    IOperationService operationService;

    /**
     * 获取金币列表数据
     * @Author Shane
     * @Date 2019/10/2515:55
     * @return
     **/
    @Override
    public Result list(HttpServletRequest request, Integer userId, Integer toUserId, Integer type, String time, Integer page, Integer limit) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(StringUtil.isEmpty(time) && userId == null && toUserId == null && type == null){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/wallet/coin/viewList")){
                data.put("coinLogs", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = userCoinLogMapper.list(p, userId, toUserId, type, beginTime, endTime);
        List<Map<String, Object>> coinLogs = iPage.getRecords();

        data.put("coinLogs", coinLogs);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "获取金币流水数据列表");

        return Result.returnSuccess(data);
    }

    /**
     * 金币统计列表
     * @Author AppiJ
     * @Date 2020/8/11 23:41
     * @param
     * @return
     */
    @Override
    public Result statisticList(HttpServletRequest request, Integer userId, String field, String sort, Integer page, Integer limit) throws Exception {

        Map<String, Object> data = new HashMap<>();

        if(userId == null){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/wallet/coin/viewStatisticList")){
                data.put("coinStatisticsList", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        if(field == null){
            field = "todayRechargeAmount";
            sort = "desc";
        }
        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = userCoinLogMapper.statisticList(p, userId, field, sort);
        List<Map<String, Object>> coinStatisticsList = iPage.getRecords();

        data.put("coinStatisticsList", coinStatisticsList);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "获取金币统计列表");

        return Result.returnSuccess(data);
    }
}
